<template>
	<view class="index_content_basic">
		<view class="index_content_basic_main">
			<el-form :model="ruleForm" :rules="rules" label-position="top" ref="ruleForm" label-width="100px"
				class="demo-ruleForm">
				<el-form-item label="项目名称" prop="projectName">
					<el-input v-model="ruleForm.projectName" placeholder="请输入项目名称"></el-input>
				</el-form-item>
				<el-form-item label="项目起止时间" prop="">
					<view class="flex">
						<el-form-item label="" prop="experienceStartTime">
							<el-date-picker type="date" placeholder="开始时间" format="yyyy-MM-dd"
								value-format="yyyy-MM-dd HH:mm:ss" v-model="ruleForm.experienceStartTime"
								style="width: 100%;">
							</el-date-picker>
						</el-form-item>
						<text style="margin: 0px 10px 0px 10px;">-</text>
						<el-form-item label="" prop="experienceEndTime">
							<el-date-picker type="date" placeholder="结束时间" format="yyyy-MM-dd"
								value-format="yyyy-MM-dd HH:mm:ss" v-model="ruleForm.experienceEndTime"
								style="width: 100%;">
							</el-date-picker>
						</el-form-item>
					</view>
				</el-form-item>
				<el-form-item label="担任职位" prop="positionName">
					<el-input v-model="ruleForm.positionName" placeholder="请输入担任职位"></el-input>
				</el-form-item>
				<el-form-item label="项目描述" prop="projectDescription">
				   <el-input type="textarea" v-model="ruleForm.projectDescription" rows="4" placeholder="请输入项目描述"></el-input>
				 </el-form-item>
			</el-form>
		</view>
		<view class="position_footer flex aic jcsb">
			<el-button class="blue_btn" style="width: 100%;" type="primary" @click="submitForm('ruleForm')">保存</el-button>
			<el-button class="" style="width: 100%;" type="danger" @click="deleteFun()" v-if="type == 0">删除
			</el-button>
		</view>
	</view>
</template>

<script>
	import {
		editResume
	} from "@/api/my.js"
	export default {
		data() {
			var validateTime = (rule, value, callback) => {
				if (!this.ruleForm.experienceStartTime) {
					callback(new Error("请选择开始时间！"))
				} else if (Date.parse(this.ruleForm.experienceStartTime) >= Date.parse(value)) {
					callback(new Error("结束时间必须大于开始时间！"))
				} else {
					callback();
				}
			};
			return {
				ruleForm: {},
				rules: {
					projectName: [{
						required: true,
						message: '请输入项目名称',
						trigger: 'blur'
					}],
					experienceStartTime: [{
						required: true,
						message: '请输入开始时间',
						trigger: 'blur'
					}],
					experienceEndTime: [{
						required: true,
						message: '请输入结束时间',
						trigger: 'blur'
					}, {
						validator: validateTime,
						trigger: ['blur', 'change']
					}, ],
					positionName: [{
						required: true,
						message: '请输入担任职位',
						trigger: 'blur'
					}],
				
					projectDescription: [{
						required: true,
						message: '请输入项目描述',
						trigger: 'blur'
					}],
				
				},
				indexValue: "",
				type: 0
			};
		},
		onLoad(options) {
			this.datas = JSON.parse(options.data)
			this.type = options.type
			if (options.type == 0) { //修改
				this.indexValue = options.index
				this.info(this.datas, options.index)
			} else { //新增	
			}
		
		},
		methods: {
			info(data, index) {
				console.log("data:", data);
				this.ruleForm = data.projectExperiences[index]
			},
			deleteFun(){
				let obj = this.datas
				obj.projectExperiences.splice(this.indexValue,1)
				editResume(obj).then(res => {
					uni.showToast({
						title: "已删除",
						icon: 'error'
					})
					if (this.type == 1) {
						uni.navigateBack("-1")
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				})
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						let obj = this.datas
						console.log("项目经历数据：",obj);
						if (this.type == 0) {
							obj.projectExperiences[this.indexValue] = this.ruleForm
						} else {
							obj.projectExperiences.push(this.ruleForm)
						}
			
						editResume(obj).then(res => {
							uni.showToast({
								title: "保存成功",
								icon: 'success'
							})
							if (this.type == 1) {
								uni.navigateBack("-1")
							}
						}).catch(res => {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						})
					} else {
						this.$message({
							message: '请完善信息',
							type: 'error',
							offset: 0.1
						});
						console.log('error submit!!');
						return false;
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.blue_btn {
		height: 40px;
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>

